package com.zhao.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.zhao.dao.inf.UserDao;
import com.zhao.entity.User;
import com.zhao.utils.EncryptUtil;

@Repository("userDao")
public class UserDaoImpl implements UserDao{
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	/**
	 * 查询用户登录信息是否正确
	 */
	@Override
	public User userLogin(User user) {
		String sql = "select id,username,password,status from user where username = ? and password = ?";
		try {
			return jdbcTemplate.queryForObject(sql,new RowMapper<User>() {

				@Override
				public User mapRow(ResultSet resultSet, int arg1) throws SQLException {
					User user = null;
					user = new User();
					user.setId(resultSet.getString("id"));
					user.setUsername(resultSet.getString("username"));
					user.setPassword(resultSet.getString("password"));
					user.setStatus(resultSet.getBoolean("status"));
					return user;
				}
			},new Object[]{user.getUsername(),EncryptUtil.SHA1(user.getPassword())});
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}
	@Override
	public void changePassword(User user, String encrpt) {
		String sql = "update user set password = ? where id = ?";
		jdbcTemplate.update(sql, new Object[]{encrpt,user.getId()});
	}

}
